/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package unit;

import dao.Impl.*;
import dao.Impl.PetDaoImpl;
import dao.Impl.StoreDaoImpl;
import dao.*;
import dao.PetDao;
import dao.StoreDao;
import entity.Pet;
import entity.Petowner;
import entity.Petstore;
import java.util.List;
import java.util.Scanner;
import service.Impl.OwnerServiceImpl;
import service.Impl.StoreServiceImpl;
import service.OwnerService;
import service.StoreService;

/**
 *
 * @author ZZ
 */
public class Client {

    public static void main(String[] args) {
        startPetShop();
    }

    //系统启动
    public static void startPetShop() {

        PetDao petDao = new PetDaoImpl();
        OwnerDao ownerDao = new OwnerDaoImpl();
        StoreDao storeDao = new StoreDaoImpl();

        List<Pet> childPet;
        List<Petowner> childOwner;
        List<Petstore> childStore;

        System.out.println("——————欢迎来到宠物开心乐园———————");
        System.out.println("——————所有宠物醒了———————");
        System.out.println("**************************************");

        childPet = petDao.select();
        for (Pet children : childPet) {
            System.out.println("第" + children.getId() + "个宠物，名字叫：" + children.getName());

        }

        System.out.println("**************************************");
        System.out.println("——————所有宠物主人醒了———————");
        System.out.println("**************************************");
        childOwner = ownerDao.select();
        for (Petowner children : childOwner) {
            System.out.println("第" + children.getId() + "个宠物主人，名字叫：" + children.getName());

        }

        System.out.println("**************************************");
        System.out.println("——————所有宠物醒商店了———————");
        System.out.println("**************************************");
        childStore = storeDao.select();
        for (Petstore children : childStore) {
            System.out.println("第" + children.getId() + "个商店，名字叫：" + children.getName());
        }
        System.out.println("**************************************");

        System.out.println("--请选择登录模式，输入1为宠物主人登录，输入2为宠物商店登录(0则退出)——");
        int selectOwnerLoginOrStoreLogin;
        Scanner scanner = new Scanner(System.in);
        selectOwnerLoginOrStoreLogin = scanner.nextInt();
        while (selectOwnerLoginOrStoreLogin == 1 || selectOwnerLoginOrStoreLogin == 2) {
            if (selectOwnerLoginOrStoreLogin == 1)//输入1为宠物主人登录
            {
                while (true) {
                    String name, password;
                    int petID;
                    int ownerID;

                    System.out.println("请输入主人账号：");
                    name = scanner.next();
                    System.out.println("请输入主人密码：");
                    password = scanner.next();
                    OwnerService ownerService = new OwnerServiceImpl();//实例化主人服务信息
                    boolean flag1 = ownerService.login(name, password);
                    if (flag1) {
                        System.out.println("----------恭喜你登录成功，以下是你的个人信息--------------");
                        childOwner = ownerDao.select();
                        for (Petowner children : childOwner) {
                            if (name.equals(children.getName())) {
                                System.out.println("名字：" + children.getName());
                                System.out.println("元宝数：" + children.getMoney());
                                break;
                            }
                        }
                        System.out.println("————登录成功，您可以购买和卖出宠物，购买宠物请输入1，卖出宠物请输入2————");
                        System.out.println("1:购买宠物" + "\n2:卖出宠物");
                        int selectBuyPetOrSellPet;
                        selectBuyPetOrSellPet = scanner.nextInt();
                        while (selectBuyPetOrSellPet == 1 || selectBuyPetOrSellPet == 2) {
                            if (selectBuyPetOrSellPet == 1) {
                                System.out.println("以下是所有库存宠物宝贝：");
                                for (Pet children : childPet) {
                                    System.out.println("第" + children.getId() + "个宠物名字叫：" + children.getName() + "\t" + "它是：" + children.getTypeName() + "\t" + "它的生日是：" + children.getBirthday() + "\n" + "健康值为：" + children.getHealth() + "\t" + "爱心值为：" + children.getLove() + "\t" + "价格为:" + children.getPrice());

                                }
                                System.out.print("请输入宠物序号选择你要购买的宠物宝贝：");
                                petID = scanner.nextInt();
                                Boolean flag2 = petDao.move(petID);
                                if (flag2) {
                                    for (Pet childrenPet : childPet) {
                                        if (petID == childrenPet.getId()) {
                                            System.out.println("---------恭喜你购买成功-----------");
                                            for (Petowner childrenOwner : childOwner) {
                                                ownerID = childrenOwner.getId();
                                                petDao.update(petID, ownerID);
                                            }

                                            break;
                                        }
                                    }
                                }
                                break;
                            } else if (selectBuyPetOrSellPet == 2)//输入2为卖出宠物
                            {
                            }

                        }

                        //剩余代码填充区
                        break;
                    } else {
                        System.out.println("——————登录失败，请确认账号密码是否正确！——————");
                    }
                }
            } else if (selectOwnerLoginOrStoreLogin == 2)//输入2为宠物商店登录
            {
                while (true) {
                    String name, password;

                    System.out.println("请输入商店账号：");
                    name = scanner.next();
                    System.out.println("请输入商店密码：");
                    password = scanner.next();
                    StoreService storeServiceService = new StoreServiceImpl();//实例化主人服务信息
                    boolean flag1 = storeServiceService.login(name, password);
                    if (flag1) {
                        System.out.println("----------恭喜你登录成功，以下是你的个人信息--------------");

                    }
                }
            }
            System.out.println("--请选择登录模式，输入1为宠物主人登录，输入2为宠物商店登录(0则退出)——");
            selectOwnerLoginOrStoreLogin = scanner.nextInt();
        }
    }

}
